﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace WorkStation.Frameworks.Core.Extensions
{
    /// <summary>
    /// * 扩展HttpRequestBase类
    /// </summary>
    public static class HttpRequestBaseExtensions
    {
        /// <summary>
        /// * 用于在Web程序中上传的文件转化为字节数组
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this HttpPostedFileBase file)
        {
            byte[] buffer = null;
            Stream stream = null;

            try
            {
                stream = (Stream)file.InputStream;

                buffer = new byte[file.ContentLength];

                stream.Read(buffer, 0, file.ContentLength);
            }
            catch (Exception ce)
            {
                throw ce;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Flush();
                    stream.Close();
                }
            }

            return buffer;
        }
    }
}
